home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Code / Chapter10 / MultiViewpointsPanel.java < prev    next >
Text File  |  2000-09-08  |  7KB  |  223 lines

  1. package applets;
  2.  
  3. import shout3d.*;
  4. import shout3d.core.*;
  5. import custom_nodes.*;
  6.  
  7.  
  8. public class MultiViewpointsPanel extends Shout3DPanel implements DeviceObserver {
  9.    
  10.    
  11.    int pixelStartX;
  12.    int pixelStartY;
  13.    
  14.    float headingSpeed = 0.0f;
  15.    float pitchSpeed = 0.0f;
  16.    float distanceSpeed = 0.0f;
  17.    float zoomPixelFactor = 0.0f;
  18.  
  19.    TargetViewpoint[] cameras = new TargetViewpoint [3];
  20.    int currentCam = 1;
  21.    int totalCams = 1;
  22.    
  23.    
  24.    float initialHeading;
  25.    float initialPitch;
  26.    float initialDistance;
  27.    float[] initialCenter;
  28.    float initialFieldOfView;   
  29.  
  30.    CameraCounter counter;
  31.  
  32.  
  33.    public MultiViewpointsPanel(Shout3DApplet applet){
  34.       super(applet);
  35.    }
  36.       
  37.    public void customInitialize() {
  38.         
  39.       addDeviceObserver(this,"DeviceInput", null);
  40.       getRenderer().addRenderObserver(this, null);
  41.       cameras[0] = (TargetViewpoint) getCurrentBindableNode("Viewpoint");
  42.       
  43.       //store initial values
  44.       initialHeading = cameras[0].heading.getValue(); 
  45.       initialPitch = cameras[0].pitch.getValue();
  46.       initialDistance = cameras[0].distance.getValue();
  47.       initialCenter = cameras[0].center.getValue();
  48.       initialFieldOfView = cameras[0].fieldOfView.getValue();
  49.       
  50.       //set zoom factor
  51.       //based on initial distance 
  52.       zoomPixelFactor = (initialDistance/10)/150;      
  53.       
  54.       
  55.       //create CameraCounter
  56.       //and add to scene
  57.       counter = new CameraCounter();
  58.       Node[]tempArray = {counter};
  59.       getScene().addChildren(tempArray);
  60.       counter.number.setValue(currentCam);
  61.    
  62.    }
  63.  
  64.  
  65.    protected void finalize()  { 
  66.       removeDeviceObserver(this,"DeviceInput");
  67.       getRenderer().removeRenderObserver(this);
  68.    }
  69.  
  70.    
  71.    
  72.    public boolean onDeviceInput(DeviceInput di, Object userData) {
  73.      
  74.       //if mouse input
  75.       if (di instanceof MouseInput) {
  76.          MouseInput mi = (MouseInput) di;
  77.          switch (mi.which){
  78.  
  79.             case MouseInput.DOWN:
  80.                pixelStartX = mi.x;
  81.                pixelStartY = mi.y;
  82.                return true;
  83.             
  84.             case MouseInput.UP:
  85.                headingSpeed = 0.0f;
  86.                pitchSpeed = 0.0f;
  87.                distanceSpeed = 0.0f;
  88.                return true;
  89.    
  90.             case MouseInput.DRAG:
  91.  
  92.                //if left button used
  93.                if(mi.button == 0) {
  94.               
  95.                   int pixelEndX = mi.x;
  96.                   int pixelEndY = mi.y;
  97.    
  98.                   int dragDistanceX = pixelEndX - pixelStartX;
  99.                   int dragDistanceY = pixelEndY - pixelStartY;
  100.                
  101.                   headingSpeed = dragDistanceX/70f;
  102.                   pitchSpeed = dragDistanceY/70f;             
  103.          
  104.                   return true;
  105.                }
  106.  
  107.                  //if right button used
  108.                  if(mi.button == 1) {
  109.  
  110.                   int pixelEndY = mi.y;
  111.                   int dragDistanceY = pixelEndY - pixelStartY;
  112.                   distanceSpeed = dragDistanceY*zoomPixelFactor;
  113.                     return true;
  114.                }
  115.          
  116.          }//end of switch
  117.  
  118.          return false;
  119.          
  120.       }//end of mouse input
  121.       
  122.       
  123.  
  124.  
  125.       //if keyboard input
  126.       if (di instanceof KeyboardInput) {
  127.  
  128.          KeyboardInput ki = (KeyboardInput) di;
  129.          if (ki.which == KeyboardInput.PRESS) {
  130.             if (ki.key == 'c' || ki.key == 'C') {
  131.  
  132.                //create new camera and
  133.                //add it to the array
  134.                if (totalCams < 3) {
  135.                
  136.                   totalCams++;
  137.                   currentCam = totalCams;
  138.                   cameras[currentCam-1] = new TargetViewpoint(this);
  139.  
  140.                   //add to scene and bind
  141.                   Node [] kids = {cameras[currentCam-1]};
  142.                   getScene().addChildren(kids);
  143.                   
  144.                   //set to initial values
  145.                   cameras[currentCam-1].isBound.setValue(true);
  146.                   cameras[currentCam-1].heading.setValue(initialHeading);
  147.                   cameras[currentCam-1].pitch.setValue(initialPitch);
  148.                   cameras[currentCam-1].distance.setValue(initialDistance);
  149.                   cameras[currentCam-1].center.setValue(initialCenter);
  150.                   cameras[currentCam-1].fieldOfView.setValue(initialFieldOfView);  
  151.                   
  152.                   //update CameraCounter display                                                  
  153.                   counter.number.setValue(currentCam);
  154.  
  155.                }         
  156.                return true;
  157.             }
  158.  
  159.  
  160.             if (ki.key == '1') {
  161.                if (totalCams >=  1) {
  162.                
  163.                   currentCam  =  1;
  164.                   cameras[currentCam- 1].isBound.setValue(true);
  165.                   counter.number.setValue(currentCam); 
  166.                }         
  167.                return true;
  168.             }
  169.  
  170.  
  171.             if (ki.key == '2') {
  172.                if (totalCams >=  2) {
  173.  
  174.                   currentCam  =  2;
  175.                   cameras[currentCam- 1].isBound.setValue(true);
  176.                   counter.number.setValue(currentCam);
  177.                }         
  178.                return true;
  179.             }
  180.  
  181.  
  182.             if (ki.key == '3') {
  183.                if (totalCams >=  3) {
  184.  
  185.                   currentCam  =  3;
  186.                   cameras[currentCam- 1].isBound.setValue(true);
  187.                   counter.number.setValue(currentCam);
  188.                }         
  189.                return true;
  190.             }
  191.  
  192.             return false;
  193.          }//end of key PRESS
  194.  
  195.          return false;
  196.  
  197.       }//end of keyboard input
  198.  
  199.       return false;
  200.  
  201.    }//end of onDeviceInput()
  202.    
  203.    
  204.  
  205.    public void onPreRender (Renderer r, Object o) {
  206.       
  207.       float headingDelta = headingSpeed/getFramesPerSecond();
  208.       float temp = cameras[currentCam-1].heading.getValue() - headingDelta;
  209.       cameras[currentCam-1].heading.setValue(temp);
  210.  
  211.       float pitchDelta = pitchSpeed/getFramesPerSecond();      
  212.        temp = cameras[currentCam-1].pitch.getValue() - pitchDelta;
  213.       cameras[currentCam-1].pitch.setValue(temp);
  214.       
  215.       float distanceDelta = distanceSpeed/getFramesPerSecond();         
  216.       temp = cameras[currentCam-1].distance.getValue() + distanceDelta;
  217.       cameras[currentCam-1].distance.setValue(temp);
  218.    
  219.    }//end of onPreRender()
  220.    
  221.  
  222. }//end of class   
  223.